package com.woniu.auth.controller;

import com.woniu.auth.dto.RoleDTO;
import com.woniu.auth.entity.SysRole;
import com.woniu.auth.service.SysPermissionService;
import com.woniu.auth.service.SysRoleService;
import com.woniu.auth.vo.TreeNodeVO;
import com.woniu.common.util.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author: WangHeng
 * @Date: 2022/12/9 15:49
 */
@RestController
@RequestMapping("roles")
@Api(value = "用户接口", tags = "AuthApi")
public class SysRoleController {
    /**
     * @Description 查询用户当前的角色以及全部角色
     * @Para a
     * @Retun a
     * @Author WangMr
     * @Date Create by 2022/12/9 17:14
     */
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysPermissionService permissionService;

    /**
     * @Description 通过用户查询拥有的角色以及查询所有的角色
     * @Para a
     * @Retun a
     * @Author WangMr
     * @Date Create by 2022/12/10 17:50
     */
    @GetMapping("{uid}")
    public Result getRolesByUser(@PathVariable("uid") Integer uid) {
        List<Integer> checkedRoles = roleService.getRolesByUser(uid);
        List<SysRole> rolesList = roleService.getAllRoles();
        return Result.ok().data("checkedRoles", checkedRoles).data("rolesList", rolesList);
    }

    /**
     * @Description 删除当前用户的角色 赋予新角色
     * @Parm a
     * @Retrn a
     * @Author WangMr
     * @Date Create by 2022/12/9 17:14
     */
    @PutMapping
    public Result updateRoles(@RequestBody RoleDTO roleDTO) {
        int i = roleService.updateRoles(roleDTO);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * @Description 得到所有的角色列表
     * @Parm a
     * @Retrn a
     * @Author WangMr
     * @Date Create by 2022/12/10 18:09
     */
    @GetMapping("getAllRoles")
    public Result getAllRoles(){
        List<SysRole> roleList = roleService.getAllRoles();
        return Result.ok().data("roleList", roleList);
    }
    @GetMapping("persTree/{rid}")
    public Result getPermissionTree(@PathVariable Integer rid){
        List<Integer> checkedkeys = roleService.getPermissionByRole(rid);
        List<TreeNodeVO> treeList = permissionService.getPermissionByParentId(0);
        return Result.ok().data("checkedkeys", checkedkeys).data("treeList", treeList);
    }
}
